package com.digital.welcome.controller;


import com.alibaba.fastjson.JSON;
import com.digital.welcome.annotation.OperationLog;
import com.digital.welcome.entity.Fee;
import com.digital.welcome.entity.Pay;
import com.digital.welcome.entity.User;
import com.digital.welcome.response.Result;
import com.digital.welcome.service.PayService;
import com.digital.welcome.service.UserService;
import com.digital.welcome.token.JwtToken;
import com.digital.welcome.util.JwtUtil;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author 韩
 */
@RestController
@RequestMapping("/pay")
public class PayController {

    @Autowired
    private PayService payService;

    @Autowired
    private UserService userService;

    @ApiOperation("缴费查询")
    @RequestMapping(value = "/select", method = RequestMethod.GET)
    public String getPay(@RequestHeader JwtToken jwtToken) {
        String token = (String) jwtToken.getPrincipal();
        Claims claims = JwtUtil.parseJWT(token);
        assert claims != null;
        String userName = claims.getId();
        User user = userService.getUserByUsername(userName);
        Pay pay = payService.selectPayByUserName(user.getUserName());
        if (pay == null) {
            return JSON.toJSONString(new Result<>().setCode(400).setMessage("你还未缴费"));
        } else {
            return JSON.toJSONString(new Result<>().setCode(200).setMessage("查询缴费成功").setData(pay));
        }
    }


    @OperationLog()
    @ApiOperation("网上缴费")
    @RequestMapping(value = "/do", method = RequestMethod.POST)
    public String pay(@RequestBody Fee fee, @RequestHeader JwtToken jwtToken) {
        String token = (String) jwtToken.getPrincipal();
        Claims claims = JwtUtil.parseJWT(token);
        assert claims != null;
        String userName = claims.getId();
        return payService.payByUserName(fee, userName);

    }

    @OperationLog()
    @ApiOperation("现场缴费")
    @RequiresPermissions("pay")
    @RequestMapping(value = "/scene-do", method = RequestMethod.POST)
    public String scenePay(@RequestBody User user) {
        return payService.scenePay(user);
    }

    @ApiOperation("获取已缴费人数")
    @RequestMapping(value = "/select-all-number", method = RequestMethod.GET)
    public String getAllNumber() {
        return payService.getPayNumber();
    }

    @ApiOperation("获取所有已缴费学生")
    @RequestMapping(value = "/get-students", method = RequestMethod.GET)
    public String getStudents() {
        return payService.getStudents();
    }

    @ApiOperation("获取所有未缴费学生")
    @RequestMapping(value = "/get-n-students", method = RequestMethod.GET)
    public String getNStudents() {
        return payService.getNStudents();
    }

    @ApiOperation("获取某学院已缴费学生")
    @RequestMapping(value = "/get-college-students", method = RequestMethod.GET)
    public String getCollegeStudents(String college) {
        return payService.getCollegeStudents(college);
    }

    @ApiOperation("获取某学院未缴费学生")
    @RequestMapping(value = "/get-college-n-students", method = RequestMethod.GET)
    public String getCollegeNStudents(String college) {
        return payService.getCollegeNStudents(college);
    }

    @ApiOperation("是否缴费")
    @RequestMapping(value = "/confirm", method = RequestMethod.POST)
    public String pay(String userName) {
        return payService.selectConfirm(userName);
    }


}
